package br.pucrs.lognplay.entity;

import java.io.Serializable;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public abstract class GenericEntity<I extends Serializable> implements Serializable {

	private static final long serialVersionUID = 1L;

	public abstract I getId();

	public boolean equals(Object object) {
		if (object == null) {
			return false;
		}

		if (object == this) {
			return true;
		}

		Class<?> objectClass = object.getClass();

		if (!objectClass.equals(getClass())) {
			return false;
		}

		@SuppressWarnings("unchecked")
		GenericEntity<I> entity = (GenericEntity<I>) object;

		EqualsBuilder equalsBuilder = new EqualsBuilder();
		equalsBuilder.append(entity.getId(), getId());
		return equalsBuilder.isEquals();
	}

	public int hashCode() {
		HashCodeBuilder hashCodeBuilder = new HashCodeBuilder();
		hashCodeBuilder.append(getId());
		return hashCodeBuilder.toHashCode();
	}

	public String toString() {
		ToStringBuilder toStringBuilder = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
		toStringBuilder.append("id", getId());
		return toStringBuilder.toString();
	}
}
